//index.js
//获取应用实例
const Api = require('../../api/api.js')
const app = getApp()
var sliderWidth = 96; // 需要设置slider的宽度，用于计算中间位置
Page({
  data: {
    //头部导航
    activeIndex: 0,
    sliderOffset: 0,
    sliderLeft: 0,
    inputShowed: false,
    inputVal: "",
    navlist: [{
      title: '默认排序',
      type: ''
    },
    {
      title: '销量优先',
      type: 'orderIndex'
    },
    {
      title: '价格优先',
      type: 'price'
    }],
    userinfo: '',
    sort: '',
    response: { data: [] }
  },
  onLoad: function () {
    let that = this;
    //初始化导航
    wx.createSelectorQuery().select('#slideBar').boundingClientRect(function (rect) {
      rect.width   // 节点的宽度
    }).exec(function (res) {
      sliderWidth = res[0].width;
      wx.getSystemInfo({
        success: function (res) {
          that.setData({
            sliderLeft: (res.windowWidth / that.data.navlist.length - sliderWidth) / 2,
            sliderOffset: res.windowWidth / that.data.navlist.length * that.data.activeIndex
          });
        }
      });
    })
    this.login()
  },
  onShareAppMessage: function () {
    console.log('index onShareAppMessage')
    return {
      title: '自定义分享标题',
      desc: '自定义分享描述',
      path: '/page/user?id=123'
    }
    // return custom share data when user share.
  },
  login: function () {
    let that = this;
    wx.showToast({
      title: '加载中',
      icon: '请求用户信息',
    })
    app.getUserInfo(function (user) {
      console.log('++++++++++++获取用户信息++++++++++++++')
      console.log(user)
      that.setData({
        userinfo: user
      })
      wx.hideToast();
      that.bindData()
    })
  },
  //绑定首页数据
  bindData: function (e) {
    console.log('++++++++++++绑定首页数据++++++++++++++')
    console.log(e)
    let pagethis = this;
    Api.getTicketList({
      data: app.addParams({
        sort: pagethis.data.sort
      }),
      success: (res) => {
        console.log(res.data)
        pagethis.setData({
          response: res.data
        })
      },
      fail: (res) => {
        console.log('++++++++++++错误信息++++++++++++++')
        console.log(res)
      }
    })

  },
  tabClick: function (e) {
    var sorttype = e.currentTarget.dataset.sorttype;
    this.setData({
      sort: sorttype,
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id
    })
    this.bindData()
  },
  showInput: function () {
    this.setData({
      inputShowed: true
    });
  },
  hideInput: function () {
    let that = this;
    that.setData({
      inputVal: "",
      inputShowed: false
    });
    that.searchList(that.data.inputVal)
  },
  clearInput: function () {
    let that = this;
    that.setData({
      inputVal: ""
    });
    that.searchList(that.data.inputVal)
  },
  inputTyping: function (e) {
    let that = this;
    that.setData({
      inputVal: e.detail.value
    });
    that.searchList(that.data.inputVal)
  },
  //搜索
  searchList: function (value) {
    let pagethis = this;
    Api.getTicketList({
      data: app.addParams({
        sort: pagethis.data.sort,
        searchKey: value
      }),
      success: (res) => {
        console.log(res.data)
        pagethis.setData({
          response: res.data
        })
      },
      fail: (res) => {
        console.log('++++++++++++错误信息++++++++++++++')
        console.log(res)
      }
    })
  }
})
